﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Data;

namespace DataAccess
{

    public class NhanVienDAO
    {
        private static readonly String URL = ConfigurationSettings.AppSettings["Main.ConnectionString"];

        public NhanVienDAO() { }


        public static DataSet CheckLogin(string username,string password)
        {
            SqlParameter[] sqlParameter = new SqlParameter[2];
            sqlParameter[0] = new SqlParameter("@TenDangNhap", username);
            sqlParameter[1] = new SqlParameter("@MatKhau", password);
            return SqlHelper.ExecuteDataset(URL, CommandType.Text, "SELECT * FROM NhanVien WHERE TenDangNhap = @TenDangNhap AND MatKhau = @MatKhau",sqlParameter);
        }

        public static DataSet GetAll()
        {
            return SqlHelper.ExecuteDataset(URL, CommandType.Text, "SELECT * FROM NhanVien");
        }

        public static void Delete(int id)
        {
            SqlParameter[] sqlParameter = new SqlParameter[1];
            sqlParameter[0] = new SqlParameter("@MaNhanVien", id);
            SqlHelper.ExecuteNonQuery(URL, CommandType.Text, "DELETE FROM NhanVien WHERE MaNhanVien = @MaNhanVien", sqlParameter);
        }

        public static void Insert(string tendangnhap,string matkhau, string hoten,string email, string sodienthoai,string diachi, int quyenhan,int tinhtrang)
        {
            SqlParameter[] sqlParameter = new SqlParameter[8];
            sqlParameter[0] = new SqlParameter("@TenDangNhap", tendangnhap);
            sqlParameter[1] = new SqlParameter("@MatKhau", matkhau);
            sqlParameter[2] = new SqlParameter("@HoVaTen", hoten);
            sqlParameter[3] = new SqlParameter("@Email", email);
            sqlParameter[4] = new SqlParameter("@SoDienThoai", sodienthoai);
            sqlParameter[5] = new SqlParameter("@DiaChi", diachi);
            sqlParameter[6] = new SqlParameter("@QuyenHan", quyenhan);
            sqlParameter[7] = new SqlParameter("@TinhTrang", tinhtrang);

            SqlHelper.ExecuteNonQuery(URL, CommandType.Text, "INSERT INTO NhanVien VALUES(@TenDangNhap,@MatKhau,@HoVaTen,@Email,@SoDienThoai,@DiaChi,@QuyenHan,@TinhTrang)", sqlParameter);
        }

        public static void Update(int id, string tendangnhap, string matkhau, string hoten, string email, string sodienthoai, string diachi, int quyenhan, int tinhtrang)
        {
            SqlParameter[] sqlParameter = new SqlParameter[9];
            sqlParameter[0] = new SqlParameter("@TenDangNhap", tendangnhap);
            sqlParameter[1] = new SqlParameter("@MatKhau", matkhau);
            sqlParameter[2] = new SqlParameter("@HoVaTen", hoten);
            sqlParameter[3] = new SqlParameter("@Email", email);
            sqlParameter[4] = new SqlParameter("@SoDienThoai", sodienthoai);
            sqlParameter[5] = new SqlParameter("@DiaChi", diachi);
            sqlParameter[6] = new SqlParameter("@QuyenHan", quyenhan);
            sqlParameter[7] = new SqlParameter("@TinhTrang", tinhtrang);
            sqlParameter[8] = new SqlParameter("@MaNhanVien", id);
            SqlHelper.ExecuteNonQuery(URL, CommandType.Text, "UPDATE NhanVien SET TenDangNhap = @TenDangNhap,MatKhau = @MatKhau,HoVaTen = @HoVaTen,Email = @Email,SoDienThoai = @SoDienThoai,DiaChi = @DiaChi,QuyenHan = @QuyenHan,TinhTrang = @TinhTrang WHERE MaNhanVien = @MaNhanVien", sqlParameter);
        }
        public static DataSet GetBy(int id)
        {
            SqlParameter[] sqlParameter = new SqlParameter[1];
            sqlParameter[0] = new SqlParameter("@MaNhanVien", id);
            return SqlHelper.ExecuteDataset(URL, CommandType.Text, "SELECT * FROM NhanVien WHERE MaNhanVien = @MaNhanVien", sqlParameter);
        }
    }
}
